﻿using System;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;

namespace Sw_MyAddin.SLDDRW
{
    class SW_Sync_properties
    {
        static ModelDoc2 swModel;
        static DrawingDoc swDraw;
        static ModelDoc2 swRefModel;
        static CustomPropertyManager swCustPropMgr;
        public static void Function(ISldWorks swApp) { Getpartproperties(swApp); }
        private static void Getpartproperties(ISldWorks swApp)//根据视图模型复制属性到工程图
        {
            swModel = swApp.ActiveDoc;
            swDraw = (DrawingDoc)swModel; // 需要在工程图模式下运行
            View swView = swDraw.GetFirstView();           

            //是否有模型
            while (swView != null)
            {
                //获取在绘图视图中引用的模型的名称。
                //Console.WriteLine(swView.Name);               
                string strRefModelPath = swView.GetReferencedModelName();
                if (strRefModelPath != "")
                {
                    if (strRefModelPath.Substring(strRefModelPath.Length - 3) == "ASM")
                    {
                        //获取此绘图视图的根组件(多零件时,获得首个视图的零件对象属性.不过一般都是一图一零件)
                        DrawingComponent swDrawComp = swView.RootDrawingComponent;
                        if (swDrawComp != null)
                        {
                            Component2 swComp = swDrawComp.Component;
                            swRefModel = swComp.GetModelDoc2();
                            swCustPropMgr = swRefModel.Extension.CustomPropertyManager[""];
                            CopyCustProps();
                        }
                    }
                    else
                    {
                        swApp.DocumentVisible(false, 1);
                        //swRefModel = swApp.OpenDoc6(strRefModelPath, 1, 1, "", _, _);
                        swRefModel = swApp.OpenDoc(strRefModelPath, 1);
                        swCustPropMgr = swRefModel.Extension.CustomPropertyManager[""];
                        CopyCustProps();
                    }
                }
                swView = swView.GetNextView();
            }
        }
        private static void CopyCustProps()
        {
            object vPropNamesObject = null;
            object vPropTypes = null;
            object vPropValues = null;
            //获得零件的属性
            swCustPropMgr.GetAll(ref vPropNamesObject, ref vPropTypes, ref vPropValues);
            swCustPropMgr = swModel.Extension.CustomPropertyManager[""];

            string[] vCompCustPropNames = (string[])vPropNamesObject;
            int[] vCompCustPropTypes = (int[])vPropTypes;
            string[] vCompCustPropValues = (string[])vPropValues;

            for (int i = 0; i < vCompCustPropNames.Length; i++)
            {
                swCustPropMgr.Delete2(vCompCustPropNames[i]);
            }
            for (int i = 0; i < vCompCustPropNames.Length; i++)
            {
                swCustPropMgr.Add2(vCompCustPropNames[i], vCompCustPropTypes[i], vCompCustPropValues[i]);
            }
        }
    }
}
